package softtech.apps.callblocker;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import softtech.apps.utility.ApplicationConstants;
import android.app.Activity;
import android.content.Intent;
import android.graphics.RadialGradient;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class ActivityHistorySetting extends Activity {

	/**
	 * Radiogroup content Time save history
	 */
	private RadioGroup rgHistory;
	private Button btnSaveSettingHistory , btnCancelSetingHistory;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_history_setting);
		getFormWidget();
		getEvent();

	}

	/**
	 * write data into file name ApplicationConstants.FILE_LIMITED
	 * @param day
	 */
	public void writeData(String day) {
		try {
			FileOutputStream out = openFileOutput(
					ApplicationConstants.FILE_LIMITED, MODE_PRIVATE);
			OutputStreamWriter writer = new OutputStreamWriter(out);
			writer.write(day);
			writer.close();
		} catch (FileNotFoundException fe) {
			fe.printStackTrace();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	public void getFormWidget() {
		rgHistory = (RadioGroup) findViewById(R.id.rgHistory);
		btnSaveSettingHistory = (Button) findViewById(R.id.btnSaveSetingHistory);
		btnCancelSetingHistory = (Button) findViewById(R.id.btnCancelSetingHistory);
	}

	public void getEvent() {

		btnSaveSettingHistory.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				int idRg = rgHistory.getCheckedRadioButtonId();
				switch (idRg) {
				case R.id.rbOneDay:
					writeData("1");
					Toast.makeText(ActivityHistorySetting.this, "1", Toast.LENGTH_SHORT).show();
					break;
				case R.id.rbTwoDay:
					Toast.makeText(ActivityHistorySetting.this, "2", Toast.LENGTH_SHORT).show();
					writeData("2");
					break;
				case R.id.rbOneWeek:
					writeData("7");
					break;
				case R.id.rbFifteen:
					writeData("15");
					break;
				case R.id.rbOneMonth:
					writeData("30");
					break;
				default:
					break;
				}
				
				Intent i = new Intent(ActivityHistorySetting.this, ActivitySettings.class);
				startActivity(i);
				ActivityHistorySetting.this.finish();
			}
		});
		
		btnCancelSetingHistory.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ActivityHistorySetting.this.finish();
			}
		});
		
		rgHistory.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				
				switch (checkedId) {
				case R.id.rbOneDay:
					break;

				default:
					break;
				}
			}
		});
	}

}
